/**
 * 
 */
package com.xbang.attendance.web.action.system.maintenance;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.system.MaintenanceStation;
import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.service.RegionService;
import com.xbang.attendance.service.SystemService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.struts.support.BaseActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class MaintenanceManager extends BaseActionSupport {

	private static final long serialVersionUID = 6843098369301424790L;

	private static final Log logger = LogFactory.getLog(MaintenanceManager.class);

	@Autowired
	private SystemService systemService;
	@Autowired
	private RegionService regionService;

	private MaintenanceStation maintenanceStation;

	private List<Warehouse> chooseWarehouses = new ArrayList<Warehouse>();

	private List<Region> regions = new ArrayList<Region>();

	private List<String> warehousIds = new ArrayList<String>();

	private Integer id;

	@Action("/system/maintenance/add")
	public String add() throws Exception {
		chooseWarehouses = systemService.getObjects(Warehouse.class);
		
	
		regions = regionService.getSGMRegion();
		return "detail";
	}

	@Action("/system/maintenance/${id}/delete")
	public String delete() throws Exception {
		if (null != id) {
			systemService.delete(MaintenanceStation.class, id);
		}
		return "r:/system/maintenance/list";
	}

	@Action("/system/maintenance/${id}/edit")
	public String edit() throws Exception {

		chooseWarehouses = systemService.getObjects(Warehouse.class);
		
		//regions = systemService.getObjects(Region.class);
		regions = regionService.getSGMRegion();

		if (null == maintenanceStation && id != null) {
			maintenanceStation = (MaintenanceStation) systemService.getObject(MaintenanceStation.class, id);
		}

		for (Warehouse warehouse : chooseWarehouses) {
			warehouse.setTmpMaintenanceStation(maintenanceStation);
		}
		return "detail";
	}

	@Action("/system/maintenance/save")
	public String save() throws Exception {
		if (null != maintenanceStation) {
			if (warehousIds != null && !warehousIds.isEmpty()) {
				Set<Warehouse> tmpWarehouse = new HashSet<Warehouse>();
				for (String warehouseId : warehousIds) {
					tmpWarehouse.add((Warehouse) systemService.getObject(Warehouse.class, Integer.valueOf(warehouseId)));
				}
				maintenanceStation.setWarehouse(tmpWarehouse);
			}
			if (maintenanceStation.getId() == null) {
				systemService.save(maintenanceStation);
			} else {
				systemService.update(maintenanceStation);
			}
		}
		return "r:/system/maintenance/"+currPageId+"/index/";
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the maintenanceStation
	 */
	public MaintenanceStation getMaintenanceStation() {
		return maintenanceStation;
	}

	/**
	 * @param maintenanceStation
	 *            the maintenanceStation to set
	 */
	public void setMaintenanceStation(MaintenanceStation maintenanceStation) {
		this.maintenanceStation = maintenanceStation;
	}

	/**
	 * @param systemService
	 *            the systemService to set
	 */
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	/**
	 * @return the chooseWarehouses
	 */
	public List<Warehouse> getChooseWarehouses() {
		return chooseWarehouses;
	}

	/**
	 * @param chooseWarehouses
	 *            the chooseWarehouses to set
	 */
	public void setChooseWarehouses(List<Warehouse> chooseWarehouses) {
		this.chooseWarehouses = chooseWarehouses;
	}

	/**
	 * @return the regions
	 */
	public List<Region> getRegions() {
		return regions;
	}

	/**
	 * @param regions
	 *            the regions to set
	 */
	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}

	/**
	 * @return the warehousIds
	 */
	public List<String> getWarehousIds() {
		return warehousIds;
	}

	/**
	 * @param warehousIds
	 *            the warehousIds to set
	 */
	public void setWarehousIds(List<String> warehousIds) {
		this.warehousIds = warehousIds;
	}

}
